package com.micro.ai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.ai.auth.entity.DictI18n;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 字典国际化Mapper接口
 *
 * @author micro-ai
 * @since 2025-01-29
 */
@Mapper
public interface DictI18nMapper extends BaseMapper<DictI18n> {

    /**
     * 根据字典数据ID查询国际化数据
     *
     * @param dictDataId 字典数据ID
     * @return 国际化数据列表
     */
    @Select("SELECT * FROM dict_i18n WHERE dict_data_id = #{dictDataId} ORDER BY locale ASC")
    List<DictI18n> selectByDictDataId(@Param("dictDataId") String dictDataId);

    /**
     * 根据字典数据ID和语言代码查询国际化数据
     *
     * @param dictDataId 字典数据ID
     * @param locale 语言代码
     * @return 国际化数据
     */
    @Select("SELECT * FROM dict_i18n WHERE dict_data_id = #{dictDataId} AND locale = #{locale}")
    DictI18n selectByDictDataIdAndLocale(@Param("dictDataId") String dictDataId, @Param("locale") String locale);

    /**
     * 根据字典类型编码和语言代码查询国际化数据
     *
     * @param dictTypeCode 字典类型编码
     * @param locale 语言代码
     * @param tenantId 租户ID
     * @return 国际化数据列表
     */
    @Select("SELECT di.* FROM dict_i18n di " +
            "LEFT JOIN dict_data dd ON di.dict_data_id = dd.id " +
            "LEFT JOIN dict_types dt ON dd.dict_type_id = dt.id " +
            "WHERE dt.code = #{dictTypeCode} AND di.locale = #{locale} " +
            "AND dd.status = 'active' AND dd.deleted_at IS NULL " +
            "AND dt.status = 'active' AND dt.deleted_at IS NULL " +
            "AND (dt.tenant_id = #{tenantId} OR dt.tenant_id IS NULL) " +
            "ORDER BY dd.sort_order ASC")
    List<DictI18n> selectByDictTypeCodeAndLocale(@Param("dictTypeCode") String dictTypeCode,
                                                 @Param("locale") String locale,
                                                 @Param("tenantId") String tenantId);
}
